//
//  SuperTableViewController.m
//  Mango
//
//  Created by lanou3g on 16/5/8.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "SuperTableViewController.h"
#import "BaseModel.h"
#import "BaseCell.h"
#import "InfoModel.h"
#import "InfoCell.h"
#import "DetailViewController.h"
#import <SVProgressHUD.h>

//列表页URL
#define LIST_URL @"http://api.app.happyjuzi.com/article/list/channel"

@interface SuperTableViewController ()
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (assign, nonatomic) NSUInteger page;
@property (nonatomic, strong) InfoModel *infoModel;
@property (nonatomic, copy) NSString *currentSkinModel;//夜间模式
@end

@implementation SuperTableViewController

- (NSInteger)ID {
    return 0;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataSource = [NSMutableArray new];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //注册cell
    [self.tableView registerNib:[UINib nibWithNibName:@"LeftModelCell" bundle:nil] forCellReuseIdentifier:@"LeftModel"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TopModelCell" bundle:nil] forCellReuseIdentifier:@"TopModel"];
    [self.tableView registerNib:[UINib nibWithNibName:@"InfoCell" bundle:nil] forCellReuseIdentifier:@"InfoCell"];
    
    [self refreshData];

}


-(void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateSkinModel) name:SkinModelDidChangedNotification object:nil];
    [self updateSkinModel];
}

-(void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark --private Method--更新皮肤模式 接到模式切换的通知后会调用此方法
-(void)updateSkinModel {
    self.currentSkinModel = [[NSUserDefaults standardUserDefaults] stringForKey:CurrentSkinModelKey];
    if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {//夜间模式
        self.tableView.backgroundColor = [UIColor blackColor];
    } else {//日间模式
        self.tableView.backgroundColor = [UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:0.7];
    }
    [self.tableView reloadData];
}




//请求网络数据
#pragma mark - 加载数据 刷新数据

- (void)loadDataFromServerWithPage:(NSUInteger)page {
    NSDictionary *paramDict = @{@"res":@"1080x1920",@"ver":@"2.11",@"channel":@"yingyongbao",@"accesstoken":@"e43c576b3de11eeeb758223cb56752c1",@"mac":@"",@"uid":@"3971092394255935",@"startup":@1,@"pf":@"android",@"page":@(page),@"id":@(self.ID),@"net":@"wifi",@"ts":@0};
    
    
    [[AFHTTPSessionManager manager]GET:LIST_URL parameters:paramDict progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //判断如果是请求第一页数据,就删除数组中元素
        if (page == 1) {
            [self.dataSource removeAllObjects];
        }
        
        NSDictionary *dataDic = responseObject[@"data"];
        
        //每页的头条
        if (dataDic[@"info"]) {
            NSDictionary *dict = dataDic[@"info"][0];
            self.infoModel = [[InfoModel alloc]init];
            [self.infoModel setValuesForKeysWithDictionary:dict];
        }
        //每页列表
        for (NSDictionary *itemDic in dataDic[@"list"]) {
            BaseModel *baseModel = [BaseModel baseModelWithDictionary:itemDic];
            //若不是头条 且 不是视频 加入数组
            if ( (baseModel.ID != self.infoModel.ID) && !baseModel.live) {
                [self.dataSource addObject:baseModel];
            }
        }
        [self.tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

//添加刷新
- (void)refreshData {
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewData)];
    self.tableView.mj_header.automaticallyChangeAlpha = YES;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
}
//获取新数据
- (void)loadNewData {
    
    self.page = 1;
    [self loadDataFromServerWithPage:self.page];
}
//加载更多数据
- (void)loadMoreData {
    
    self.page++;
    [self loadDataFromServerWithPage:self.page];
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataSource.count + 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        InfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"InfoCell"];
        cell.model = self.infoModel;
#pragma mark --private Method--更新皮肤模式
        if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {//夜间模式
            [cell updateToNightSkinMode];
        }else {
            [cell updateToDaySkinMode];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else {
        BaseCell *cell = [BaseCell cellWithTableView:tableView andIndexPath:indexPath andModel:self.dataSource[indexPath.row - 1]];
#pragma mark --private Method--更新皮肤模式
        if ([self.currentSkinModel isEqualToString:NightSkinModelValue]) {//夜间模式
            [cell updateToNightSkinMode];
        }else {
            [cell updateToDaySkinMode];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return [InfoCell heightForHeadlineModelCell];
    } else {
        BaseModel *baseModel = self.dataSource[indexPath.row - 1];
        return baseModel.cellHeight;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    DetailViewController *detailVC = [[DetailViewController alloc]init];
    if (indexPath.row == 0) {
        NSString *urlRoute = [self.infoModel.urlroute componentsSeparatedByString:@"?"][0];
        if ([urlRoute isEqualToString:@"juzi://specialreport"]) {
            return;
        }
        detailVC.ID = self.infoModel.ID;
        detailVC.listmodel = self.infoModel;
    }else {
        BaseModel *model = self.dataSource[indexPath.row - 1];
        NSString *urlRoute = [model.urlroute componentsSeparatedByString:@"?"][0];
        if ([urlRoute isEqualToString:@"juzi://specialreport"]) {
            return;
        }
        detailVC.ID = model.ID;
        detailVC.listmodel = model;
    }

    [self.navigationController pushViewController:detailVC animated:YES];
}



@end
